<template>
    <div style="width: 100%;">
        <div v-if="active==1">
            <el-form :inline="true">
                <el-form-item>
                    <el-select v-model="params.subId" :value="params.subId">
                        <el-option label="招标编号" value=""></el-option>
                        <el-option
                                v-for="sub in subTenderPlanList"
                                :key="sub.id"
                                :label="sub.id"
                                :value="sub.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="params.projectName" placeholder="项目名称" :clearable="true"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="params.projectType" :value="params.projectType">
                        <el-option label="项目类型" value=""></el-option>
                        <el-option
                                v-for="projectType in projectTypeList"
                                :key="projectType.typeId"
                                :label="projectType.typeName"
                                :value="projectType.typeId"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="params.companyName" placeholder="公司名称" :clearable="true"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="params.status" :value="params.status">
                        <el-option label="竞标状态" value=""></el-option>
                        <el-option label="待开标" value="1"></el-option>
                        <el-option label="已开标" value="2"></el-option>
                        <el-option label="中标" value="3"></el-option>
                        <el-option label="未中标" value="4"></el-option>
                        <el-option label="已撤销" value="5"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-left: 10px">
                    <el-button @click="handleSearch" type="primary">检索</el-button>
                    <el-button @click="handleBatchOpen" type="primary">批量开标</el-button>
                </el-form-item>
            </el-form>

            <el-table :data="pageResult.records" @selection-change="handleSelectChange">
                <el-table-column type="selection" :selectable="canSelect"></el-table-column>
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="招标编号" prop="subId"></el-table-column>
                <el-table-column label="项目名称" prop="projectName"></el-table-column>
                <el-table-column label="项目类型" prop="projectType"></el-table-column>
                <el-table-column label="报价公司" prop="companyName"></el-table-column>
                <el-table-column label="报价日期" prop="createdate"></el-table-column>
                <el-table-column label="报价金额(万)" prop="offer">
                    <template slot-scope="scope">
                        {{scope.row.offer/10000}}
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="status">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status=='1'" style="color: #cf9236;font-weight: 700">待开标</span>
                        <span v-if="scope.row.status=='2'" style="color: #20a0ff;font-weight: 700">已开标</span>
                        <span v-if="scope.row.status=='3'" style="color: green;font-weight: 700">中标</span>
                        <span v-if="scope.row.status=='4'" style="color: red;font-weight: 700">未中标</span>
                        <span v-if="scope.row.status=='5'" style="color: #8c939d;font-weight: 700">已撤销</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown @command="handleCommand" trigger="click">
                            <el-button>
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item :command="{row:scope.row,type:'open'}" v-if="scope.row.status=='1'">开标</el-dropdown-item>
                                <el-dropdown-item :command="{row:scope.row,type:'cooperation'}" v-if="scope.row.status=='2' || scope.row.status=='1'">授标</el-dropdown-item>
                                <el-dropdown-item :command="{row:scope.row,type:'company'}">查看公司信息</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <el-divider></el-divider>

            <el-footer class="case-list-footer">
                <el-pagination
                        style="margin: 10px auto"
                        background
                        layout="total , sizes ,prev, pager, next,jumper"
                        :page-sizes="[5,10,15,20]"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        @current-change="handleIndexChange"
                        @size-change="handleSizeChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <div style="width: 100%;" v-if="active==2">
            <register-company :companyId="companyId" @back="active=1"></register-company>
        </div>
    </div>
</template>

<script>
    import {SubBiddingHistoryService} from "../../../model/gs/SubBiddingHistoryService";
    import {ProjectTypeService} from "../../../model/gs/ProjectTypeService";
    import {SubTenderPlanService} from "../../../model/gs/SubTenderPlanService";

    const subBiddingHistoryService = SubBiddingHistoryService.getInstance()
    const projectTypeService = ProjectTypeService.getInstance()
    const subTenderPlanService = SubTenderPlanService.getInstance()

    const RegisterCompany = () => import('../TenderManager/RegisterCompany.vue')

    export default {
        data() {
            return {
                active:1,
                params: {
                    subId: '',
                    projectName: '',
                    projectType: '',
                    status: '',
                    companyName:''
                },
                projectTypeList: [],
                subTenderPlanList:[],
                pageResult: [],
                checkedIds: '',
                companyId:''
            }
        },
        methods: {
            getProjectTypeList() {
                projectTypeService.getList().then(response => {
                    if (response.data.code === 0) {
                        this.projectTypeList = response.data.data
                    }
                })
            },
            getSubTenderPlanList() {
                subTenderPlanService.list().then(response => {
                    if(response.data.code === 0){
                        this.subTenderPlanList = response.data.data
                    }
                })
            },
            getSubBiddingHistoryList() {
                subBiddingHistoryService.getPageList(this.params).then(response => {
                    if (response.data.code === 0) {
                        this.pageResult = response.data.data
                    }
                })
            },
            handleSearch() {
                this.params.page = 1
                this.getSubBiddingHistoryList()
            },
            handleIndexChange(index) {
                this.params.page = index
                this.getSubBiddingHistoryList()
            },
            handleSizeChange(size) {
                this.params.size = size
                this.getSubBiddingHistoryList()
            },
            handleCommand(command) {
                switch (command.type) {
                    case 'open':
                        this.handleOpen(command.row);
                        break;
                    case 'company':
                        this.handleShowCompany(command.row);
                        break;
                    case 'cooperation':
                        this.handleCooperation(command.row);
                        break;
                }
            },
            handleCooperation(row){
                subBiddingHistoryService.cooperation(row).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success({
                            message: '选择成功',
                            duration: 1000
                        })
                        this.getSubBiddingHistoryList()
                    }
                })
            },
            handleShowCompany(row) {
                this.companyId = row.companyId
                this.active++
            },
            handleOpen(row){
                subBiddingHistoryService.open(row).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'开标成功',
                            duration:1000
                        })
                        this.getSubBiddingHistoryList()
                    }
                })
            },
            handleBatchOpen() {
                if(this.checkedIds == ''){
                    this.$message.warning({
                        message:'请至少选择一项记录',
                        duration:1000
                    })
                    return
                }
                subBiddingHistoryService.batchOpen(this.checkedIds).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'开标成功',
                            duration:1000
                        })
                        this.getSubBiddingHistoryList()
                    }
                })
            },
            handleSelectChange(val) {
                this.checkedIds = ''
                if (val.length != 0) {
                    const array = val.map(item => item.id)
                    this.checkedIds = array.join(',')
                }
            },
            canSelect(row) {
                return row.status == '1'
            }
        },
        created() {
            this.getSubBiddingHistoryList()
            this.getSubTenderPlanList()
            this.getProjectTypeList()
        },
        components:{
            RegisterCompany
        }
    }
</script>

<style scoped>

</style>
